﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BR_CruzDelSur;
using BR_CruzDelSur.Interfaces;
using System.Web.Services;
using BE_CruzDelSur;
using System.Data;

namespace WebCruzDelSur
{
    public partial class BuscarEntregaCarga : System.Web.UI.Page
    {
        IBR_Carga carga = new BR_Carga();
        IBR_Util util = new BR_Util();
                
        protected void Page_Load(object sender, EventArgs e)
        {
            if (HttpContext.Current.Session["Usuario"] == null)
            {
                String message = "Debe autenticarse en el sistema. ";
                String strJavascriptFunctions = "";
                strJavascriptFunctions = "ShowMessageError('" + message + "');";
                ClientScript.RegisterStartupScript(this.GetType(), "Alerta", strJavascriptFunctions, true);
                return;
            }

            if (!Page.IsPostBack)
            {
                ddlDepartamento.DataSource = util.f_ListarUtil("", "UD", true);
                ddlDepartamento.DataValueField = "Codigo";
                ddlDepartamento.DataTextField = "Nombre";
                ddlDepartamento.DataBind();
            }
        }

        protected void gridCarga_RowCommand(object sender, GridViewCommandEventArgs e)
        {

        }

        protected void btnBuscarEntrega_Click(object sender, EventArgs e)
        {

            int departamento = Convert.ToInt16(ddlDepartamento.SelectedValue == "" ? "-1" : ddlDepartamento.SelectedValue);
            string dniDestino = txtCliente.Text;

            List<BE_Carga> lstCarga = carga.f_ListarCargasAEntregar(dniDestino, departamento);

            gridCarga.DataSource = lstCarga;
            gridCarga.DataBind();

            lblSinResultados.Visible = !(lstCarga.Count() > 0);            
        }

        protected void gridCarga_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                BE_Carga drv = null;
                int id = -1;
                ImageButton btnAlert = (ImageButton)e.Row.FindControl("btnEditar");
                drv = ((BE_Carga)e.Row.DataItem);
                id = drv.CodigoCarga;
                btnAlert.Attributes.Add("onclick", "verificarCargaAEntregar(" + id + ");return false;");                
            }
        }

    }
}